<?php

class Wxmp
{
	private $appid;
	private $apikey;
	private $secretkey;

	
	function __construct($appid,$apikey,$secretkey)
	{
		$this->appid = $appid;
		$this->apikey = $apikey;
		$this->secretkey = $secretkey;
	}
	
	/**
	 * Make an HTTP request
	 *
	 * @return string API results
	 * @ignore
	 */
	function http($url, $data = array())
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		//如果是post请求
		if( is_array($data) )
		{
			curl_setopt($ch, CURLOPT_POST, 0);
			curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
		}
		$output = curl_exec($ch);
		curl_close($ch);
		return $output;
	}

	function GetUrl($url)
	{
		//初始化curl
		$ch = curl_init();
		//设置超时
		curl_setopt($ch, CURLOPT_TIMEOUT, 10);
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,FALSE);
		curl_setopt($ch, CURLOPT_HEADER, FALSE);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		
		//运行curl，结果以json形式返回
		$res = curl_exec($ch);
		curl_close($ch);
		//取出openid
		$data = json_decode($res,true);
		return $data;
	}
	
	/**
	* 获得微信登录地址
	* @param 参数1，回调地址。
	*/
	function CreateLoginUrl($backurl,$login_type)
	{
		if($login_type==1){
			//在用户授权后获取用户数据
			$url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->appid."&redirect_uri=".urlencode($backurl)."&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect";
		}else{
			//不用获取用户信息信息
			$url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->appid."&redirect_uri=".urlencode($backurl)."&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
		}
		
		header('location:'.$url);
		exit();
		//return $url;
	}
	
	
	/**
	* 获得access_token
	*/
	function GetToken()
	{
		$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->appid.'&secret='.$this->secretkey.'&code='.$_GET['code'].'&grant_type=authorization_code';
		$data = json_decode($this->http($url),true);
		return $data;
	}

	function GetAccessToken()
	{
		
		$url ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" .$this->appid. "&secret=" .$this->secretkey;
		$data = json_decode($this->http($url),true);
		return $data;
	}

	/**
	 * 获得用户openid
	 * @param 参数1，选填，1为静默授权方式，2为手动授权方式
	 */
	function GetOpenid($backurl=null,$type = '1')
	{
		//静默授权方式
		if( $type == '1' )
		{
			$scope = 'snsapi_base';
		}
		//用户手动授权
		else
		{
			$scope = 'snsapi_userinfo';
		}

		//通过code获得openid
		if (!isset($_GET['code']))
		{
			
			//触发微信返回code码
			$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$this->appid.'&redirect_uri='.urlencode($backurl).'&response_type=code&scope='.$scope.'&state=STATE#wechat_redirect';
			Header("Location: $url");
			exit();
		}
		else
		{
			//创建获得openid的url
			$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->appid.'&secret='.$this->secretkey.'&code='.$_GET['code'].'&grant_type=authorization_code';
			$data = $this->GetUrl($url);
			//$openId = $data['openid'];
		}
		return $data;
	}
	
	/**
	* 获得用户信息
	*/
	function GetUserInfo($access_token,$openid)
	{
		/**
		//$result = $this->GetToken();
		//验证成功
		if( !empty($result['access_token']) )
		{
			$url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$result['access_token'].'&openid='.$result['openid'].'&grant_type=snsapi_userinfo';
			$data = json_decode($this->http($url),true);
			return $data;
		}
		else
		{
			return $result;
		}
		**/
		if(!empty($access_token)){
			//$url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&grant_type=snsapi_userinfo';
			//$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
			$url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';
			                file_put_contents('/tmp/xx.log', $url, FILE_APPEND);

			$data = json_decode($this->http($url),true);
			return $data;
		}
	}
}
